/**
 * 实时数据定时刷新
 */
var jq=jQuery.noConflict();
var waterPlantCode;//水厂编码
var waterPlantName;//水厂名称
var backwashPumpNum,backwashFanNum,supplyPumpNum,getPumpNum;
var waterPlantTypeCH="";
var waterPlantTypeEN="";

jQuery(document).ready(function()
{	
	waterPlantCode = parent.waterPlantCode;
	waterPlantName = parent.waterPlantName;
	jq("#waterPlantRealDataName").html(waterPlantName+"厂区实时数据表");
	jq("#stationWaterNameStr").html(waterPlantName+"管网监测实时数据表");
	
	getAllNum();	
	getAllWaterPlantTypeCHAndEN();
	
	getStationRealTimeData();
	getWaterPlantRealTimeData();
	autoRefresh(300*1000);//300s刷新
	
	
});

function getAllNum()
{
	jq.ajax({			
		url:'../MonitorData/GetBackwashPumpNum.action?waterPlantCode='+waterPlantCode,
		type:'POST',
		data:"{}",
		async:false,//同步
		dataType:'text',
		success:function (data)
		{			
			//alert("backwashPumpNum="+data);
			backwashPumpNum = data;
		}
	});
	jq.ajax({			
		url:'../MonitorData/GetBackwashFanNum.action?waterPlantCode='+waterPlantCode,
		type:'POST',
		data:"{}",
		async:false,//同步
		dataType:'text',
		success:function (data)
		{			
			//alert("backwashPumpNum="+data);
			backwashFanNum = data;
		}
	});
	jq.ajax({			
		url:'../MonitorData/GetSupplyPumpNum.action?waterPlantCode='+waterPlantCode,
		type:'POST',
		data:"{}",
		async:false,//同步
		dataType:'text',
		success:function (data)
		{			
			//alert("backwashPumpNum="+data);
			supplyPumpNum = data;
		}
	});
	jq.ajax({			
		url:'../MonitorData/GetGetPumpNum.action?waterPlantCode='+waterPlantCode,
		type:'POST',
		data:"{}",
		async:false,//同步
		dataType:'text',
		success:function (data)
		{			
			//alert("backwashPumpNum="+data);
			getPumpNum = data;
		}
	});
}
/**
 * 定时刷新
 * @param timeInterval
 */
function autoRefresh(timeInterval)
{
	
	setInterval(function()
			{
				getWaterPlantRealTimeData();
				getStationRealTimeData();
			},timeInterval);
}

/**
 * 获取厂区实时信息
 */
function getWaterPlantRealTimeData()
{
	jq.ajax({			
		url:'../MonitorData/GetWaterPlantRealTimeData.action?waterPlantCode='+waterPlantCode,
		type:'POST',
		data:"{}",		
		dataType:'json',
		success:function (data)
		{	//alert(JSON.stringify(data));

			jq.each(data,function(index,content)
			{
				if(index==0)
				{
					jq("#waterPlantRealDataTime").html("采集时间："+content.time);
				}
				//根据各个泵的个数动态生成表格
				var table="";
				
				table = "<tr align='center'> " +
						"<td><b>类型</b></td> <td bgcolor=\"#61d9d8\"><b > 值</b></td>" +
						"<td><b>类型</b></td> <td bgcolor=\"#61d9d8\"><b > 值</b></td>" +
						"<td><b>类型 </b></td> <td bgcolor=\"#61d9d8\"><b > 值</b></td>" +
						"<td><b>类型</b></td> <td bgcolor=\"#61d9d8\"><b > 值</b></td> " +
						"</tr>";
				var j=0;
				for(var i=0;i<waterPlantTypeCH.length;)
				{ 
					var str = waterPlantTypeEN[i+1];

					if(str.indexOf("backwashPump")!=-1 && (parseInt(str.charAt(str.length - 1))>backwashPumpNum))
					{
						//alert("backwashPumpNum");
						i++;
					}
					else if(str.indexOf("backwashFan")!=-1 && (parseInt(str.charAt(str.length - 1))>backwashFanNum))
					{
						i++;
					}
					else if((str.indexOf("rotatingSpeed")!=-1 || str.indexOf("frequency")!=-1 ||str.indexOf("supplyState")!=-1)&& (parseInt(str.charAt(str.length - 1))>supplyPumpNum))
					{
						i++;
					}
					else if(str.indexOf("getState")!=-1 && (parseInt(str.charAt(str.length - 1))>getPumpNum))
					{
						i++;
					}
					else
					{
						j++;	
						if((j%4)==1)
						{
							table += "<tr align='center'>"
						}				
							table+="<td>"+waterPlantTypeCH[i]+"</td>"
							table+="<td bgcolor=\"#61d9d8\">"+content[str]+"</td>"
						
						if((j%4)==0)
						{
							table+="</tr>"
						}
						i++;
					}
				}
				//alert(table);
				document.getElementById("WaterPlantRealtableBody").innerHTML=table;
				
						
			});
			
			
			
			
		}
		});
}

function getAllWaterPlantTypeCHAndEN()
{	
	jq.ajax({			
		url:'../MonitorData/GetAllTypeName.action',
		type:'POST',
		data:"{}",
		async:false,//同步
		dataType:'json',
		success:function (data)
		{  
			waterPlantTypeCH = data;
		}
	});
	jq.ajax({			
		url:'../MonitorData/GetAllWaterPlantTypeNameEN.action',
		type:'POST',
		data:"{}",
		async:false,//同步
		dataType:'json',
		success:function (data)
		{  
			waterPlantTypeEN = data;
		}
	});
}

/**
 * 获取管网实时信息
 */
function getStationRealTimeData()
{
	jq.ajax({			
		url:'../MonitorData/GetRealStationDataAction.action?waterPlantCode='+waterPlantCode,
		type:'POST',
		data:"{}",
		dataType:'json',
		success:function (data)
		{
			jq("#stationRealTimeDataTable").bootstrapTable('load',data);
		}
		});
}
